package com.littlepay.kr4.paulin.dfeba77.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 77dfeba on 2017/5/26.
 * 日期工具，功能有：获取当前时间，获取地区
 * 精确到：天，用于列表日期展示
 *         秒,用于区别某一消费数据
 */

public class DateUtil {
    private DateUtil(){}
    //private String todayDate;//精确到日
    //private String nowDate;//精确到时分秒
    //获取当前时间,精确到时分秒
    public static String getNowDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", LocalUtil.getLocal());
        return dateFormat.format(new Date());
    }
    //获取当前时间,精确到日
    public static String getTodayDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", LocalUtil.getLocal());
        return dateFormat.format(new Date());
    }
    /**该类适用于对sticker显示适配器的使用
     * @param time 传入sticker记录的详细时间 格式为 yyyy-MM-dd HH:mm:ss
     * @return 返回转换成为的日期*/
    public static String timeToDate(String time){
        //虽然是10为因该是0-9，但是endIndex应该是指结束后面一位
        //功能 yyyy-MM-dd HH:mm:ss 转换成 yyyy-MM-dd
        return time.substring(0,10);
    }

    public static String getMonthDate(){
        return getTodayDate().substring(0,7);
    }
    public static int getMonthDays(){
        int month = Integer.parseInt(DateUtil.getTodayDate().substring(6,7));
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                month = 31;
                break;
            case 2:
            case 4:
            case 6:
            case 9:
            case 11:
                month = 30;
                break;
            default:
                month = 30;
                break;

        }
        return month;
    }
    public static int getMonthLeftDay(){
        return DateUtil.getMonthDays() - Integer.parseInt(DateUtil.getTodayDate().substring(9,10));
    }
    public static String getPreviousDate(String todayDate){
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(timeToDate(todayDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE,day - 1);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar
                .getTime());
    }
    public static String getLastMonth(String currentMonth){
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(timeToDate(currentMonth));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH) - 1);
        //仅仅返回月份，不要精确到天
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar
                .getTime());
    }
    /*获取传入日期的与今日日期差值，精确到天
    * @param Date 传入的日期，格式能够自动转换
    * @return 相差天数 int*/
    public static int getIntervalDays(String Date){
        if(Date.length() == 19){
            Date = timeToDate(Date);
        }
        //int interval = 0;
        Date date1 = null;
        Date date2 = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", LocalUtil.getLocal());
        try {
            date1 =dateFormat.parse(Date);
            date2 =dateFormat.parse(getTodayDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //判断处理情况，出错默认返回差值0天
        if (date1 == null || date2 ==null){
            return  0;
        }else {
            return (int) ((date2.getTime() - date1.getTime())/ (1000 * 60* 60 * 24));
        }
    }
}
